package com.edu.zju.scs.panacea.controller.resource;

import com.edu.zju.scs.panacea.common.Global;
import com.edu.zju.scs.panacea.entity.BaseResult;
import com.edu.zju.scs.panacea.entity.resource.Dev;
import com.edu.zju.scs.panacea.entity.resource.Position;
import com.edu.zju.scs.panacea.entity.resource.Site;
import com.edu.zju.scs.panacea.service.resource.DevService;
import com.edu.zju.scs.panacea.service.resource.SiteService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @Author: Yue Liang
 * @Date: 2018-12-13 20:10
 */

@Controller
@EnableAutoConfiguration
public class DevController {
    @Autowired
    private DevService devService;

    @ResponseBody
    @ApiOperation(value="根据id查询某个设备", notes="根据id查询某个设备")
    @RequestMapping(value = "/dev/{id}", method = RequestMethod.GET)
    public BaseResult getDev(@PathVariable Long id) {
        return devService.getDev(id);
    }


    @ResponseBody
    @ApiOperation(value="查询某个站点下设备", notes="查询某个站点下设备")
    @RequestMapping(value = "/dev/site/{type}", method = RequestMethod.GET)
    public BaseResult getDevBySiteId(@PathVariable int type,  HttpSession session) {
        Site site = (Site) session.getAttribute("site");
        return devService.getDevBySiteId(site.getId(), type);
    }

    @ResponseBody
    @ApiOperation(value="根据ID删除设备", notes="根据ID删除设备")
    @RequestMapping(value = "/dev/deletion/{id}", method = RequestMethod.GET)
    public BaseResult deleteDev(@PathVariable Long id) {
        return devService.deleteDev(id);
    }

    @ResponseBody
    @ApiOperation(value="新增设备", notes="新增设备")
    @RequestMapping(value = "/dev/new", method = RequestMethod.POST)
    public BaseResult addDev(
            @ApiParam("自有ID") @RequestParam(value = "ownId") String ownId,
            @ApiParam("设备名称") @RequestParam(value = "name") String name,
            @ApiParam("wifi探针标识") @RequestParam(value = "probeId") String probeId,
            @ApiParam("设备高度") @RequestParam(value = "height") Float height,
            @ApiParam("设备宽度") @RequestParam(value = "width") Float width,
            @ApiParam("设备物理经度") @RequestParam(value = "longitude") Float longitude,
            @ApiParam("设备物理纬度") @RequestParam(value = "latitude") Float latitude,
            @ApiParam("设备类型") @RequestParam(value = "type") Integer type,
            @ApiParam("设备所在国家") @RequestParam(value = "country") String country,
            @ApiParam("设备所在省份") @RequestParam(value = "province") String province,
            @ApiParam("设备所在城市") @RequestParam(value = "city") String city,
            @ApiParam("设备所在区/县") @RequestParam(value = "district") String district,
            HttpSession session) {
        Site site = (Site) session.getAttribute("site");

        Dev dev = new Dev();
        dev.setOwnId(ownId);
        dev.setName(name);
        dev.setProbeId(probeId);
        dev.setSiteId(site.getId());
        dev.setHeight(height);
        dev.setWidth(width);
        dev.setLongitude(longitude);
        dev.setLatitude(latitude);
        dev.setType(type);

        Position position = new Position();
        position.setCountry(country);
        position.setProvince(province);
        position.setCity(city);
        position.setDistrict(district);
        return devService.addDev(dev, position);
    }




    @ResponseBody
    @ApiOperation(value="修改设备信息", notes="修改设备信息")
    @RequestMapping(value = "/dev/edit", method = RequestMethod.POST)
    public BaseResult editDev(
            @ApiParam("ID") @RequestParam(value = "id") Long id,
            @ApiParam("自有ID") @RequestParam(value = "ownId") String ownId,
            @ApiParam("设备名称") @RequestParam(value = "name") String name,
            @ApiParam("设备高度") @RequestParam(value = "height") Float height,
            @ApiParam("设备宽度") @RequestParam(value = "width") Float width,
            @ApiParam("设备物理经度") @RequestParam(value = "longitude") Float longitude,
            @ApiParam("设备物理纬度") @RequestParam(value = "latitude") Float latitude,
            @ApiParam("设备类型") @RequestParam(value = "type") Integer type,
            @ApiParam("设备所在国家") @RequestParam(value = "country") String country,
            @ApiParam("设备所在省份") @RequestParam(value = "province") String province,
            @ApiParam("设备所在城市") @RequestParam(value = "city") String city,
            @ApiParam("设备所在区/县") @RequestParam(value = "district") String district,
            HttpSession session) {
        Site site = (Site) session.getAttribute("site");

        Dev dev = new Dev();
        dev.setId(id);
        dev.setOwnId(ownId);
        dev.setName(name);
        dev.setSiteId(site.getId());
        dev.setHeight(height);
        dev.setWidth(width);
        dev.setLongitude(longitude);
        dev.setLatitude(latitude);
        dev.setType(type);

        Position position = new Position();
        position.setDevId(id);
        position.setCountry(country);
        position.setProvince(province);
        position.setCity(city);
        position.setDistrict(district);

        dev.setPosition(position);
        return devService.editDev(dev);
    }

}
